from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from .models import Cate, HomePage, BrowsingHistory


class CateSer(ModelSerializer):
    class Meta:
        model = Cate
        fields = '__all__'


class HomePageSer(ModelSerializer):
    ca = serializers.SerializerMethodField(read_only=True)

    def get_ca(self, obj):
        return obj.cate.name

    class Meta:
        model = HomePage
        fields = '__all__'


class BrowsingHistorySer(ModelSerializer):
    home = serializers.SerializerMethodField(read_only=True)

    def get_home(self, obj):
        return HomePageSer(obj.homepage).data

    class Meta:
        model = BrowsingHistory
        fields = '__all__'

